# coding:utf-8
'''
Created on 2015年6月30日

@author: Emrys
'''

from queue import Queue
from threading import Thread

_sentinel = object()


def producer(out_q: Queue):
    n = 10
    while n > 0:
        print("n: ", n)
        out_q.put(n)
        n -= 1

    out_q.put(_sentinel)


def consumer(in_q: Queue):
    while True:
        data = in_q.get()
        if data is _sentinel:
            # 最要和q.join()相关，表示队列已经处理完毕
            in_q.task_done()
            break
        else:
            print(data)
            # 最要和q.join()相关，表示队列已经处理完毕
            in_q.task_done()

if __name__ == "__main__":
    q = Queue(3)
    t1 = Thread(target=producer, args=(q,))
    t2 = Thread(target=consumer, args=(q,))
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    q.join()
